package pl.edu.mimuw.irs.core.model.design.net;

import java.util.Set;

import pl.edu.mimuw.irs.core.global.IDManager;
import pl.edu.mimuw.irs.core.model.design.exception.InternalDesignException;
import pl.edu.mimuw.irs.core.model.design.exception.InternalDesignException.DesignCause;
import pl.edu.mimuw.irs.core.model.design.net.refinement.Refinement;
import pl.edu.mimuw.irs.core.model.petrinet.ITransition;
import pl.edu.mimuw.irs.core.model.petrinet.Transition;
import pl.edu.mimuw.irs.core.util.Position;

/**
 * @author Jakub Rauch
 * Created on: 2009-05-07
 */
public class IRSTransition extends IRSAbstractNode<ITransition> implements ITransition {
	
	private static final long serialVersionUID = 1072788871171239189L;
	
	// *** Attributes

	private double chance;
	
	// Getters and setters
	public double getChance() {
		return chance;
	}
	public void setChance(double chance) {
		if (chance < 0) throw new InternalDesignException(DesignCause.TRANSITION_NEGATIVE_CHANCE,
				this, chance);
		this.chance = chance;
	}
	
	// *** Constructors
	
	public IRSTransition(IDManager idManager, String id, 
			Refinement refinement, Position position) {
		super(new Transition(idManager, id, position));
		refine(refinement);
		setChance(1);
	}
	
	public IRSTransition(IDManager idManager, String id, Position position) {
		this(idManager, id, Refinement.NONE, position);
	}
	
	public IRSTransition(IDManager idManager, Position position) {
		this(idManager, null, position);
	}
	
	public IRSTransition(IDManager idManager, String id) {
		this(idManager, id, new Position());
	}
	
	public IRSTransition(IDManager idManager) {
		this(idManager, (String)null);
	}
	
	/**
	 * Copying constructor.
	 * @param node
	 */
	public IRSTransition(IRSTransition transition) {
		super((IRSAbstractNode<ITransition>)transition);
	}
	
	// *** Methods

	@Override
	public Set<ITransition> getFollowingActiveTransitions() {
		return getPetriNode().getFollowingActiveTransitions();
	}

	@Override
	public boolean invoke() {
		return getPetriNode().invoke();
	}

	@Override
	public boolean isActive() {
		return getPetriNode().isActive();
	}
	
//	public List<IRSNode> getAtomicNodes() {
//		List<IRSNode> leafs = new LinkedList<IRSNode>();
//		if (getRefinement().equals(Refinement.NONE)) {
//			leafs.add(this);
//		} else {
//			for (IRSNode node : getChildren()) {
//				leafs.addAll(node.getAtomicNodes());
//			}
//		}
//		return leafs;
//	}
//
//	public List<IRSNode> getExpandedNodes() {
//		List<IRSNode> leafs = new LinkedList<IRSNode>();
//		if (!isExpanded()) {
//			leafs.add(this);
//			return leafs;
//		} else {
//			for (IRSNode node : getChildren()) {
//				IRSNode rcpNode = (IRSNode)node;
//				leafs.addAll(rcpNode.getExpandedNodes());
//			}
//		}
//		return leafs;
//	}
//		
//	public void detach() {
//		super.detach();
//		for (IRSNode child : getChildren()) {
//			child.detach();
//		}
//	}
	
	@Override
	public IRSTransition clone() {
		return new IRSTransition(this);
	}
}
